package com.feicheng.lanniao.service.impl;

import java.util.HashMap;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.lanniao.entity.PayCost;
import com.feicheng.lanniao.entity.PayOrderv2;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.PayOrderv2Mapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IPayCostService;
import com.feicheng.lanniao.service.IPayOrderv2Service;
import com.feicheng.lanniao.util.SnowflakeIdWorker;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PayOrderv2Impl extends ServiceImpl<PayOrderv2Mapper, PayOrderv2> implements IPayOrderv2Service {
	
	/**
	 * @see IPayOrderv2Service PayOrderList
	 */
	@Override
	public ResultInfo payOrderList(String searchKey,
			String order,
			Boolean dec,
			Integer orderTypeId,
			String orderNumber,
			String date,
			Integer clientId,
			Integer statu,
			Integer currentPage,
			Integer pageSize) {
		
		QueryWrapper<PayOrderv2> payOrderWrapper = new QueryWrapper<PayOrderv2>();
		
		//判断排序条件order是否为空
				if(StringUtils.hasLength(order)) {
					if (!order.equals("default")) {
						if(dec) {
							payOrderWrapper.orderByDesc(order);
						}
						else {
							payOrderWrapper.orderByAsc(order);
						}
					}
					else {
						payOrderWrapper.orderByAsc("order_id");
					}
				}
		
		
		//判断查询条件searchKey是否为空，不为空则与数据名字关联
				if(StringUtils.hasLength(searchKey)) {
					payOrderWrapper.and(i -> i.like("order_type_id", searchKey)
					.or().like("order_number", searchKey)
					.or().like("date", searchKey)
					.or().like("client_id", searchKey)
					.or().like("statu", searchKey));
				}
		
		
		//判断查询条件orderTypeId是否为空，不为空则与数据名字关联
		if (null !=orderTypeId && orderTypeId!=0) {
			payOrderWrapper.eq("order_type_id", orderTypeId);
		}
		//判断查询条件orderNumber是否为空，不为空则与数据名字关联
				if (StringUtils.hasLength(orderNumber)) {
					payOrderWrapper.like("order_number", orderNumber);
				}
		//判断查询条件date是否为空，不为空则与数据名字关联
		if (StringUtils.hasLength(date)) {

			payOrderWrapper.like("date", date.substring(0,10));
		}
		//判断查询条件clientId是否为空，不为空则与数据名字关联
				if (null !=clientId && clientId!=0) {
					payOrderWrapper.eq("client_id", clientId);
				}
		//判断查询条件statu是否为空，不为空则与数据名字关联
		if (null !=statu && statu!=0) {
			payOrderWrapper.eq("statu", statu);
		}
			
		IPage<PayOrderv2> pageInfo = new Page<PayOrderv2>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, payOrderWrapper));
	}

	/**
	 * @see IPayOrderv2Service getNotOperatedOrder
	 * 查询逻辑是按照订单类型和client查询订单状态为已支付且未操作的订单编号以及订单状态
	 */
	
	@Override
	public ResultInfo getNotOperatedOrder(String order, Boolean dec, Integer clientId, Integer orderTypeId,
			Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		QueryWrapper<PayOrderv2> notOperatedWrapper = new QueryWrapper<PayOrderv2>();
		
		//判断排序条件order是否为空
		if(StringUtils.hasLength(order)) {
			if (!order.equals("default")) {
				if(dec) {
					notOperatedWrapper.orderByDesc(order);
				}
				else {
					notOperatedWrapper.orderByAsc(order);
				}
			}
			else {
				notOperatedWrapper.orderByAsc("order_id");
			}
		}
		
		//判断查询条件clientId和orderTypeId是否为空，不为空或0则与数据名字关联
		
		if(null ==clientId ) {
			throw new SystemException("用户Id不能为空");
		}
		if(null == orderTypeId) {
			throw new SystemException("订单类型不能为空");
		}		
		
		PayOrderv2 payOrderv2 = this.getOne(notOperatedWrapper.eq("client_id", clientId).eq("order_type_id", orderTypeId).eq("statu", 2).eq("caozuo_state", "0"));
		IPage<PayOrderv2> pageInfo = new Page<PayOrderv2>(currentPage, pageSize);
		log.info("=======payOrderv2=============" + payOrderv2);
		return ResultInfo.success(payOrderv2);

		
		
		
	
	}

	/**
	 * @see IPayOrderv2Service delPayOrder
	 */
	@Override
	public ResultInfo delPayOrder(String orderNumber) {
		// TODO Auto-generated method stub
		
		//如果orderNumber为空，抛出异常
			if(!StringUtils.hasLength(orderNumber)) {
					throw new SystemException("orderNumber为空");
				}
		QueryWrapper<PayOrderv2> payOrderWrapper = new QueryWrapper<PayOrderv2>();
		payOrderWrapper.eq("order_number", orderNumber);
		
		// 根据主键查询出一条数据，用一个对象接收
		PayOrderv2 payOrderv2 = this.getOne(payOrderWrapper);
		// 设置删除位值
		payOrderv2.setStatu(3);
		// 修改订单
		Boolean b = this.updateById(payOrderv2);
		// 返回修改结果
			if (!b) {
				throw new SystemException("修改失败");
				}
			return ResultInfo.success();
	}

	
	@Autowired
	
	private IPayCostService IPayCostService;
	
	@Override
	public ResultInfo addPayOrder(Integer clientId, Integer orderTypeId) {
		// TODO Auto-generated method stub
		
		if(null== clientId || 0 == clientId ) {
			throw new SystemException("用户ID不能为空") ;
		}
		if(null== orderTypeId || 0 == orderTypeId ) {
			throw new SystemException("订单类型不能为空") ;
		}
		//根据订单类型查询每单价格
		QueryWrapper<PayCost> orderPrice = new QueryWrapper<PayCost>();
		PayCost payCost = IPayCostService.getOne(orderPrice.eq("order_type_id", orderTypeId));
		//将数据放入对象
		PayOrderv2 payOrderv2 = new PayOrderv2();
		payOrderv2.setClientId(clientId);
		payOrderv2.setOrderTypeId(orderTypeId);
		payOrderv2.setMoney(payCost.getPrice());
		payOrderv2.setOrderNumber(new SnowflakeIdWorker(1, 1).nextId() + "");
		//存入数据库
		this.save(payOrderv2);
		Map<String,Object> map = new HashMap<>();
		map.put("clientId", clientId);
		map.put("orderTypeId", orderTypeId);
		map.put("orderNumber", payOrderv2.getOrderNumber());
		map.put("price", payOrderv2.getMoney());
		return ResultInfo.success(map);
	}
	
	/**
	 * @see IPayOrderv2Service updateOrderInfo
	 * @param description
	 * @param orderNumber
	 * @param successTime
	 * @return
	 */
	@Override
	public ResultInfo updateOrderInfo(String description, String orderNumber, String successTime) {
		log.info("description={}",description);
		log.info("orderNumber={}",orderNumber);
		log.info("successTime={}",successTime);
		QueryWrapper<PayOrderv2> payOrdervWrapper = new QueryWrapper<>();
		PayOrderv2 payOrderv2 = getOne(payOrdervWrapper.eq("order_number",orderNumber));
		log.info("payOrderv2={}",payOrderv2);
		payOrderv2.setStatu(2);
		payOrderv2.setDate(successTime);
		payOrderv2.setDescription(description);
		log.info("payOrderv2===set后={}",payOrderv2);
		return ResultInfo.success(this.updateById(payOrderv2));
	}
	
	@Override
	public ResultInfo updateInfo(String orderNumber) {
		QueryWrapper<PayOrderv2> queryWrapper = new QueryWrapper<PayOrderv2>();
		PayOrderv2 payOrderv2 = this.getOne(queryWrapper.eq("order_number", orderNumber));
		payOrderv2.setCaozuoState("1");
		this.updateById(payOrderv2);
		return ResultInfo.success();
	}

	@Override
	public ResultInfo getInfo(String orderNumber) {
		// TODO Auto-generated method stub
		QueryWrapper<PayOrderv2> queryWrapper = new QueryWrapper<PayOrderv2>();
		PayOrderv2 payOrderv2 = this.getOne(queryWrapper.eq("order_number", orderNumber));
		return ResultInfo.success(payOrderv2.getStatu());
	}

}
